package com.ymatou.query.seller.trading.api.parameter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.YMTDateUtil;

/**
 * 存储过程: 交易服务SP->sp_trd_GetSellerSalesInfo_v2
 * 
 * @author songyefeng
 *
 */
@SuppressWarnings("rawtypes")
public class Sp_GetSellerSalesInfo_v2 {
	private int sellerId;
	private String date = null;

	public void setSellerId(int sellerId) {
		this.sellerId = sellerId;
	}

	public void setDate(String date) {
		this.date = date;
	}

	private String getSalesQueryField(String day) {
		StringBuffer sb = new StringBuffer();
		sb.append(" '" + day + "' as TimeSpan,");
		sb.append(" sum(IIF(i.iSalesType = 1, i.fProductPrice * i.iAmount - isnull(i.fSellerCouponAmount,0) - isnull(i.fSellerPromotionAmount,0) , 0)) Spot,");
		sb.append(" sum(IIF(i.iSalesType = 2 or i.iSalesType is null, i.fProductPrice * i.iAmount - isnull(i.fSellerCouponAmount,0) - isnull(i.fSellerPromotionAmount,0), 0)) Shangou,");
		sb.append(" sum(isnull(i.fSellerCouponAmount,0)) SellerCoupon");
		return sb.toString();
	}

	private String getSalesJoinCondtion() {
		return " join ymt_orderinfo i with(nolock) on o.iOrderId = i.iOrderId";
	}

	private String getSalesQueryCondition(String begin, String end) {
		return " o.iBuyerId = " + sellerId + " and o.dAddTime >= '" + begin
				+ "' and o.dAddTime < '" + end
				+ "' and o.iTradingStatus in (1,2,3,4,16,17)";
	}

	private String getDealsQueryField(String day) {
		StringBuffer sb = new StringBuffer();
		sb.append(" '" + day + "' as TimeSpan,");
		sb.append(" sum(IIF(i.iSalesType = 1, i.fProductPrice * i.iAmount + isnull(i.fDiscount, 0) + isnull(i.fFreight, 0) - isnull(i.fSellerCouponAmount,0) - isnull(i.fSellerPromotionAmount,0), 0)) Spot,");
		sb.append(" sum(IIF(i.iSalesType = 2 or i.iSalesType is null, i.fProductPrice * i.iAmount + isnull(i.fDiscount, 0) + isnull(i.fFreight, 0) - isnull(i.fSellerCouponAmount,0) - isnull(i.fSellerPromotionAmount,0), 0)) Shangou,");
		sb.append(" sum(isnull(i.fSellerCouponAmount,0)) SellerCoupon");
		return sb.toString();
	}

	private String getDealsJoinCondtion() {
		return getSalesJoinCondtion();
	}

	private String getDealsQueryCondition(String begin, String end) {
		return " o.iBuyerId = " + sellerId
				+ " and o.iTradingStatus = 4 and o.dConfirmedTime >= '" + begin
				+ "' and o.dConfirmedTime < '" + end + "'";
	}

	public void execute() {
		YmtOrdersWapper wapper = new YmtOrdersWapper();

		String format = YMTDateUtil.YMD;
		if (DataUtil.Stringg.isNullOrEmpty(date)) {
			date = YMTDateUtil.getSysTime();
		}
		String today = YMTDateUtil.addDays(0, format);
		String yesterday = YMTDateUtil.addDays(-1, format);
		String tomorrow = YMTDateUtil.addDays(1, format);
		String week = YMTDateUtil.calendarToString(
				com.ymttest.common.entitie.TimeRange.getFirstDayOfThisWeek(),
				format);
		String month = YMTDateUtil.calendarToString(
				com.ymttest.common.entitie.TimeRange.getFirstDateOfThisMonth(),
				format);
		List<Map> sales_zuotian = wapper.selectOrders2(
				getSalesQueryField("yesterday"), getSalesJoinCondtion(),
				getSalesQueryCondition(yesterday, today));
		List<Map> sales_jintian = wapper.selectOrders2(
				getSalesQueryField("today"), getSalesJoinCondtion(),
				getSalesQueryCondition(today, tomorrow));
		List<Map> sales_benzhou = wapper.selectOrders2(
				getSalesQueryField("week"), getSalesJoinCondtion(),
				getSalesQueryCondition(week, tomorrow));
		List<Map> sales_benyue = wapper.selectOrders2(
				getSalesQueryField("month"), getSalesJoinCondtion(),
				getSalesQueryCondition(month, tomorrow));
		saleList.addAll(sales_zuotian);
		saleList.addAll(sales_jintian);
		saleList.addAll(sales_benzhou);
		saleList.addAll(sales_benyue);
		List<Map> deals_zuotian = wapper.selectOrders2(
				getDealsQueryField("yesterday"), getDealsJoinCondtion(),
				getDealsQueryCondition(yesterday, today));
		List<Map> deals_jintian = wapper.selectOrders2(
				getDealsQueryField("today"), getDealsJoinCondtion(),
				getDealsQueryCondition(today, tomorrow));
		List<Map> deals_benzhou = wapper.selectOrders2(
				getDealsQueryField("week"), getDealsJoinCondtion(),
				getDealsQueryCondition(week, tomorrow));
		List<Map> deals_benyue = wapper.selectOrders2(
				getDealsQueryField("month"), getDealsJoinCondtion(),
				getDealsQueryCondition(month, tomorrow));
		dealList.addAll(deals_zuotian);
		dealList.addAll(deals_jintian);
		dealList.addAll(deals_benzhou);
		dealList.addAll(deals_benyue);
	}

	/**
	 * 销售额
	 */
	private List<Map> saleList = new ArrayList<Map>();

	/**
	 * 成交额
	 */
	private List<Map> dealList = new ArrayList<Map>();
	
	public List<Map> getSaleList(){
		return saleList;
	}
	
	public List<Map> getDealList(){
		return dealList;
	}
	
	public static void main(String[] args) {
		Sp_GetSellerSalesInfo_v2 sp = new Sp_GetSellerSalesInfo_v2();
		sp.setSellerId(3383);
		sp.execute();
		System.out.println(sp.getSaleList());
		System.out.println(sp.getSaleList().size());
		System.out.println(sp.getDealList());
		System.out.println(sp.getDealList().size());
	}

}
